package com.zte.purchase.server.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zte.purchase.dao.IProductDao;
import com.zte.purchase.model.CProduct;
import com.zte.purchase.model.ProductCondition;
import com.zte.purchase.server.IProductServer;
import com.zte.util.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServer implements IProductServer {

    @Autowired
    private IProductDao productDao;


    @Override
    public boolean addProduct(CProduct cProduct) {
        return productDao.addProduct(cProduct);
    }

    @Override
    public boolean updateProduct(CProduct cProduct) {
        return productDao.updateProduct(cProduct);
    }

    @Override
    public CProduct findOneProduct(String productId) {
        return productDao.findOneProduct(productId);
    }

    @Override
    public PageModel<CProduct> queryProductList(PageModel<CProduct> pageModel, ProductCondition cProduct) {
        int pageNum = pageModel.getPageNum()<=1?1:pageModel.getPageNum();
        int pageSize = pageModel.getPageSize() <= 0 ? 5 : pageModel.getPageSize();
        PageHelper.startPage(pageNum,pageSize);

        List<CProduct> cProducts = productDao.queryProductList(cProduct);
        PageInfo<CProduct> info = new PageInfo<>(cProducts);
        return PageModel.getPageModel(pageModel,info);
    }
}
